#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "myxpm.h"
#include "transf2d.h"

#define NROPT  9
#define NRSIZE 4

/*
  Proto

  XPM *initXPM(uint32 width, uint32 height, uint8 cpp, uint32 ncolors);
  void freeXPM(XPM *imag);
  int putXPMpixel(XPM *imag, uint32 x, uint32 y, uint32 colorindex);
  int setXPMcolor(XPM *imag, uint32 index, uint8 r, uint8 g, uint8 b, char *charpattern);
  int writeXPMtofile(XPM *imag, char *filepath);
*/

char opt[NROPT][NRSIZE] = { "-f", "-w", "-h", "-o", "-wl", "-wt", "-wr", "-wb", "-t" };
int opt_taken[NROPT];

int opt_test()
{
    int i;
    for (i = 0; i < NROPT; i++)
    {
        if (opt_taken[i] == 0)
            return 0;
    }

    return 1;
}

void usage(int code, char *str)
{
    printf("[%d] Invalid arguments.\n", code);
    printf("Usage:  egioc.exe  -f <file_in> -w <width> -h <height> -o <file_out>\n");
    printf("                   -wl <xmin> -wt <ymin> -wr <xmax> -wb <ymax> -t <tsf_file>\n");
    exit(1);
}

int main(int argc, char *argv[])
{
    int i, j, cond;
    uint32 width, height, wl, wt, wr, wb;
    char *fname_in, *fname_out, *tsf_file;

    //=========================================================
    if (argc < 19) usage(0, argv[0]);

    for (i = 1; i < argc; i = i + 2)
    {
        cond = 0;
        for (j = 0; j < NROPT; j++)
        {
            if (strcmp(argv[i], opt[j]) == 0)
            {
                opt_taken[j] = 1;
                cond = 1;
                switch(j)
                {
                case 0:
                    fname_in = argv[i + 1];
                    break;
                case 1:
                    width = atoi(argv[i + 1]);
                    break;
                case 2:
                    height = atoi(argv[i + 1]);
                    break;
                case 3:
                    fname_out = argv[i + 1];
                    break;
                case 4:
                    wl = atoi(argv[i + 1]);
                    break;
                case 5:
                    wt = atoi(argv[i + 1]);
                    break;
                case 6:
                    wr = atoi(argv[i + 1]);
                    break;
                case 7:
                    wb = atoi(argv[i + 1]);
                    break;
                case 8:
                    tsf_file = argv[i + 1];
                    break;
                }
            }
        }
        if (!cond) usage(1, argv[0]);
    }
    if (!opt_test()) usage(2, argv[0]);
    //=========================================================
    Mat3x3 a;
    if ( (read_tsf(tsf_file, &a)) == 0 )
        matrix_display(&a);
    else
    {
        printf("Unable to open tsf file!\n");
        exit(1);
    }

    Window *win;
    win = setWindow(wl, wt, wr, wb);

    printf("%d\n", ps_to_xpm(fname_in, fname_out, width, height, win, &a));
    clearWindow(win);

    return 0;
}
